package com.example.Wisdompark.controller;

import com.example.Wisdompark.pojo.User;
import com.example.Wisdompark.service.UserService;
import com.example.Wisdompark.utils.CookieUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录流程处理，包括自动登录和输入密码登录、退出登录等功能的处理
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private CookieUtils cookieUtils;

    @RequestMapping("/indexLogin")
    public String forwardLogin(
            HttpServletRequest request,
            Model model
    ) {

        // 如果有cookie，那么就根据cookie中的账户登录
        Cookie useraccount = cookieUtils.findCookies("useraccount", request.getCookies());
        Cookie userpwd = cookieUtils.findCookies("userpwd", request.getCookies());
        if (useraccount != null && userpwd != null) {

            String account = useraccount.getValue();
            String pwd = userpwd.getValue();

            String result = userService.queryUserPwdByAccount(account);
            if (result != null && result.equals(pwd)) {
                User user = userService.queryUserByAccount(account);
                model.addAttribute("user",user);
                return "User";
            }
        }

        // 没有cookie 返回到登录界面
        return "Login";
    }


    // 重定向
    @RequestMapping("/index")
    public String toIndex(){
        return "index";
    }


    // 用户登录
    @PostMapping("/user/login")
    public String login(
            @Param("useraccount") String useraccount,
            @Param("userpwd") String userpwd,
            HttpServletResponse response,
            HttpServletRequest request,
            Model model) {

        String pwd = userService.queryUserPwdByAccount(useraccount);
        if (pwd != null && pwd.equals(userpwd)) {
            User user = userService.queryUserByAccount(useraccount);


            Cookie accountCookie = cookieUtils.createCookie("useraccount", user.getAccount(),
                    60 * 60, request.getContextPath());
            Cookie pwdCookie = cookieUtils.createCookie("userpwd", user.getPassword(),
                    60 * 60, request.getContextPath());
            response.addCookie(accountCookie);
            response.addCookie(pwdCookie);
            model.addAttribute("user", user);
            return "User";
        } else if (pwd == null) {
            model.addAttribute("errorAccount", "用户名不正确");
        } else {
            model.addAttribute("errorPassword", "密码错误");
        }


        model.addAttribute("account", useraccount);
        model.addAttribute("pwd", userpwd);

        return "Login";
    }


    // 退出登录功能 删除cookie
    @RequestMapping("/logout")
    public String logout(HttpServletResponse response,
                         HttpServletRequest request) {
        Cookie accountCookie = cookieUtils.createCookie("useraccount", "",
                0, request.getContextPath());
        Cookie pwdCookie = cookieUtils.createCookie("userpwd", "",
                0, request.getContextPath());
        response.addCookie(accountCookie);
        response.addCookie(pwdCookie);
        return "forward:/";
    }

    // 跳转到登录界面
    @RequestMapping("/Signup")
    public String SignUP(){
        return "signup";
    }
    // 注册功能
    @PostMapping("/signup")
    public String signUp(
            @Param("useraccount") String useraccount,
            @Param("userpwd") String userpwd,
            @Param("name") String name,
            HttpServletResponse response,
            HttpServletRequest request,
            Model model
    ) {
        userService.insertUser(useraccount, userpwd, name);
        Cookie accountCookie = cookieUtils.createCookie("useraccount", useraccount,
                60 * 60, request.getContextPath());
        Cookie pwdCookie = cookieUtils.createCookie("userpwd", userpwd,
                60 * 60, request.getContextPath());
        response.addCookie(accountCookie);
        response.addCookie(pwdCookie);
        User user = userService.queryUserByAccount(useraccount);
        model.addAttribute("user", user);
        return "User";
    }
}
